import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'


const routes = [
    //定义路由
    {
        //路由path
        path: '/',
        //路由name
        name: 'home',
        //路由组件
        component: () => import('@/view/home.vue')
    },
    {
        path: '/other',
        name: 'other',
        component: () => import('@/view/other.vue')
    },
    {
        path: '/course',
        name: 'course',
        component: () => import('@/view/course.vue')
    },
    {
        path: '/CourseDetails:id',
        name: 'coursedetails',
        component: () => import('@/view/CourseDetails.vue')
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/view/login.vue')
    },
    {
        path: '/shopping',
        name: 'shopping',
        component: () => import('@/view/shopping.vue'),
        beforeEnter:(to,from,next)=>{
            if(localStorage.getItem('msg')){
                next()
            }else{
                next("/login")
                ElMessage({
                    showClose: true,
                    message: '你还未登入 请先登入',
                    type: 'success',
                })
            }
        }
    },
]

// 创建路由对象
const router = createRouter({
    //hash模式的路由
    history: createWebHashHistory(),
    routes
})

export default router
